package cn.ulc.socket.weapp.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.util.Result;
import cn.ulc.socket.weapp.model.param.AddressSaveParam;
import cn.ulc.socket.weapp.service.AddressServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/wechat/address")
public class AddressController {
    private static final Log log = LogFactory.get();

    @Autowired
    AddressServ addressServ;

    //得到地址列表
    @GetMapping("/")
    public Result findList(){
        return Result.success(addressServ.findList());
    }
    //保收货地址
    @PostMapping("")
    public Result save(@Validated @RequestBody AddressSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存收货地址信息有误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(addressServ.save(param));
    }
    //更新商品到购物车
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody AddressSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存收货地址信息有误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(addressServ.update(id,param));
    }
    //详情
    @GetMapping("/{id}")
    public Result findDetail(@PathVariable("id") Long id){
        return Result.success(addressServ.findDetail(id));
    }
    //详情
    @GetMapping("/findDefaultAddress")
    public Result findDefaultAddress(){
        return Result.success(addressServ.findDefaultAddress());
    }
    //得到省市列表
    @GetMapping("/findCityList")
    public Result findCityList(){
        return Result.success(addressServ.findCityList());
    }

    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(addressServ.deleted(id));
    }
}
